package cn.iocoder.yudao.module.dr.controller.admin.journalapplydetail;

import cn.iocoder.yudao.module.dr.controller.admin.journalapplydetail.vo.JournalApplyDetailPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.journalapplydetail.vo.JournalApplyDetailRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.journalapplydetail.vo.JournalApplyDetailSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dr.controller.admin.journalapplydetail.vo.*;
import cn.iocoder.yudao.module.dr.dal.dataobject.journalapplydetail.JournalApplyDetailDO;
import cn.iocoder.yudao.module.dr.service.journalapplydetail.JournalApplyDetailService;

@Tag(name = "管理后台 - 期刊申请详情")
@RestController
@RequestMapping("/dr/journal-apply-detail")
@Validated
public class JournalApplyDetailController {

    @Resource
    private JournalApplyDetailService journalApplyDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建期刊申请详情")
    @PreAuthorize("@ss.hasPermission('dr:journal-apply-detail:create')")
    public CommonResult<Long> createJournalApplyDetail(@Valid @RequestBody JournalApplyDetailSaveReqVO createReqVO) {
        return success(journalApplyDetailService.createJournalApplyDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新期刊申请详情")
    @PreAuthorize("@ss.hasPermission('dr:journal-apply-detail:update')")
    public CommonResult<Boolean> updateJournalApplyDetail(@Valid @RequestBody JournalApplyDetailSaveReqVO updateReqVO) {
        journalApplyDetailService.updateJournalApplyDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除期刊申请详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dr:journal-apply-detail:delete')")
    public CommonResult<Boolean> deleteJournalApplyDetail(@RequestParam("id") Long id) {
        journalApplyDetailService.deleteJournalApplyDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得期刊申请详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dr:journal-apply-detail:query')")
    public CommonResult<JournalApplyDetailRespVO> getJournalApplyDetail(@RequestParam("id") Long id) {
        JournalApplyDetailDO journalApplyDetail = journalApplyDetailService.getJournalApplyDetail(id);
        return success(BeanUtils.toBean(journalApplyDetail, JournalApplyDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得期刊申请详情分页")
    @PreAuthorize("@ss.hasPermission('dr:journal-apply-detail:query')")
    public CommonResult<PageResult<JournalApplyDetailRespVO>> getJournalApplyDetailPage(@Valid JournalApplyDetailPageReqVO pageReqVO) {
        PageResult<JournalApplyDetailDO> pageResult = journalApplyDetailService.getJournalApplyDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JournalApplyDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出期刊申请详情 Excel")
    @PreAuthorize("@ss.hasPermission('dr:journal-apply-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJournalApplyDetailExcel(@Valid JournalApplyDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JournalApplyDetailDO> list = journalApplyDetailService.getJournalApplyDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "期刊申请详情.xls", "数据", JournalApplyDetailRespVO.class,
                        BeanUtils.toBean(list, JournalApplyDetailRespVO.class));
    }

}